package com.swust.servlet;

import com.swust.service.LibServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "CatgoryServlet",urlPatterns = "/CatgoryServlet")
public class CatgoryServlet extends HttpServlet {

    private LibServiceImpl libService = new LibServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String catgoryName = request.getParameter("catgoryName");
        String description = request.getParameter("description");
        System.out.println(id + catgoryName + description);
        String msg = "";
        if (libService.addBookCatgory(id, catgoryName, description)) {
            // 如果添加成功
            msg = "添加成功！";
        } else {
            // 如果添加失败
            msg = "添加ID重复，请查看图书分类后重新添加。";
        }
        request.setAttribute("Msg",msg);
        request.getRequestDispatcher("/view/catgory.jsp").forward(request,response);
        return;
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
